package com.songqi.common.core.utils.file;

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;


/**
 * @author Administrator
 * @version $Id$
 * @since
 * @see
 */
public class Word2PdfUtil {

    private static final Logger log = LoggerFactory.getLogger(Word2PdfUtil.class);
    public static boolean getLicense() {
        boolean result = false;
        try {
            InputStream is = new ByteArrayInputStream(getLicenseStr().getBytes("UTF-8"));
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void doc2pdf(String inPath, String outPath) {
        log.info("word文件路径：{}", inPath);
        log.info("pdf文件路径：{}", outPath);
        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
            return;
        }
        try {
            log.info("开始转换pdf");
            long old = System.currentTimeMillis();
            File file = new File(outPath); // 新建一个空白pdf文档
            FileOutputStream os = new FileOutputStream(file);
            Document doc = new Document(inPath); // Address是将要被转化的word文档
            doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
                                         // EPUB, XPS, SWF 相互转换
            long now = System.currentTimeMillis();
            log.info("共耗时：" + ((now - old) / 1000.0) + "秒"); // 转化用时
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("转换失败：{}",e.getMessage());
        }
    }

    private static String getLicenseStr(){
        return "<License>" +
                "    <Data>" +
                "        <Products>" +
                "            <Product>Aspose.Total for Java</Product>" +
                "            <Product>Aspose.Words for Java</Product>" +
                "        </Products>" +
                "        <EditionType>Enterprise</EditionType>" +
                "        <SubscriptionExpiry>20991231</SubscriptionExpiry>" +
                "        <LicenseExpiry>20991231</LicenseExpiry>" +
                "        <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>" +
                "    </Data>" +
                "    <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>" +
                "</License>";
    }

    public static void main(String[] args) {
//        String inPath = "/var/folders/tw/ls77mfl957g6r5npvz6n1cqh0000gn/T/fa6ec972-9dce-41a8-b768-7227621dcd1a.docx";
//        String outPath = "/Users/songqi/bics-contract/1638064823766122498-CAOLO1-肠癌多基因甲基化检测·标准版--王晓晗-0217.pdf";

        String inPath = "/Users/songqi/Desktop/demo1.docx";
        String outPath = "/Users/songqi/Desktop/demo1.pdf";
        Word2PdfUtil.doc2pdf(inPath, outPath);
    }
}
